package edu.gatech.mobile.scene.ui.manage;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;
import edu.gatech.mobile.scene.AppConstants;
import edu.gatech.mobile.scene.R;
import edu.gatech.mobile.scene.model.ImageInfo;
import edu.gatech.mobile.scene.model.MaskInfo;
import edu.gatech.mobile.scene.provider.impl.ImageInfoDBImpl;
import edu.gatech.mobile.scene.provider.impl.ImageInfoTable;
import edu.gatech.mobile.scene.provider.impl.MaskInfoDBImpl;
import edu.gatech.mobile.scene.ui.image.ImageViewActivity;
/**
 * Activity for showing details of each image. Allows re-labeling of the image also.
 * @author Jing
 *
 */
public class ImageDetailActivity extends Activity implements OnClickListener{
	
	private String imagePath;
	private ImageInfo imageInfo;
	private CheckBox useCB;
	private ImageInfoDBImpl imageDB;
	private MaskInfoDBImpl maskDB;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_detail_view);
		imagePath = getIntent().getExtras().getString(ImageInfoTable.FILE_NAME);
		imageDB = new ImageInfoDBImpl(this);
		maskDB = new MaskInfoDBImpl(this);
		imageInfo = imageDB.getImageByName(imagePath);
		initViews();
		
		
	}
	
	/**
	 * Setting UI components
	 */
	private void initViews() {
		//re-label button
		Button btn = (Button) findViewById(R.id.label_button);
		btn.setOnClickListener(this);
		
		//delete button
		Button delete_btn = (Button) findViewById(R.id.delete_image);
		delete_btn.setOnClickListener(this);	
		
		//show horizon button
		Button show_horizon_btn = (Button) findViewById(R.id.show_horizon_overlay);
		show_horizon_btn.setOnClickListener(this);
		
		//read in image and scale it.
		Bitmap image = BitmapFactory.decodeFile(imageInfo.getFileName());
		if(image!= null){
		ImageView preview = (ImageView) findViewById(R.id.image_preview);
		float scaleWidth = 0.9f;
		float scaleHeight = 0.9f;
		Matrix mat = new Matrix();
		mat.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
				image.getHeight(), mat, true);
		BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
		preview.setImageDrawable(bmd);
		}
		
		//check box indicating the image is used for training or not
		useCB = (CheckBox) findViewById(R.id.use);
		if(imageInfo.isUse()){
			useCB.setChecked(true);
		}
		else{
			useCB.setChecked(false);
		}
	}
	
	
	@Override
	protected void onPause(){
		super.onPause();
		imageInfo.setUse(useCB.isChecked());
		//imageInfo.setDark(isDark.isChecked());
		imageDB.updateImage(imageInfo);
	}
	
	/**
	 * Click listeners for all the buttons in this activity
	 */
	@Override
	public void onClick(View view) {
		
		//remove all the labeled data and start ImageViewActivity for user to re-label the image
		if (view.getId() == R.id.label_button) {			
			maskDB.removeMasksForImage(imageInfo
					.getFileName());
			useCB.setChecked(true);
			Bundle bdl = new Bundle();
			bdl.putString(AppConstants.BUNDLE_IMAGE_KEY, imageInfo.getFileName());
			Intent i = new Intent(ImageDetailActivity.this,
					ImageViewActivity.class);
			i.putExtras(bdl);
			startActivity(i);
		}
		
		//delete the image and it's labeled data
		else if (view.getId() == R.id.delete_image) {

			for(MaskInfo mask : imageInfo.getMasks()){
				File delete = new File(mask.getMaskName());
				delete.delete();
			}
			maskDB.removeMasksForImage(imageInfo.getFileName());
			
			File toDelete = new File(imageInfo.getFileName());
			toDelete.delete();
			imageDB.removeImage(imageInfo);
			Toast.makeText(this, "Image "+ imageInfo.getImagePath() + " has been deleted.", Toast.LENGTH_LONG).show();
		}
		
		//view the image with horizon drawn on top
		else if (view.getId() == R.id.show_horizon_overlay){
			Bundle bdl = new Bundle();
    		bdl.putString(AppConstants.BUNDLE_IMAGE_KEY, imagePath);
    		Intent i = new Intent(ImageDetailActivity.this, ImageModelViewActivity.class);
    		i.putExtras(bdl);
    		startActivity(i);
		}

	}	
	
}
